<link href="<?php echo base_url('theme/third_party/rating/demo/css/application.css'); ?>" rel="stylesheet">
<script src="<?php echo base_url('theme/third_party/rating/lib/jquery.raty.min.js'); ?>" type="text/javascript"></script>
<script src="<?php echo base_url('theme/js/frontend/js.js'); ?>" type="text/javascript"></script>
<link href="<?php echo base_url('theme/third_party/youtubePlaylist/src/ytv.css'); ?>" rel="stylesheet">
<link href="<?php echo base_url('theme/css/frontend/search.css'); ?>" rel="stylesheet">
<script type="text/javascript" src="<?php echo base_url('theme/third_party/youtubePlaylist/src/ytv.js'); ?>"></script>
<script type="text/javascript" src="<?php echo base_url('theme/js/frontend/jquery.PrintArea.js'); ?>"></script>
<script src="<?php echo base_url('theme/third_party/rating/lib/jquery.raty.min.js'); ?>" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $.fn.raty.defaults.path = '<?php echo base_url('theme/third_party/rating/lib/img'); ?>';
        $('#create-profile').click(function() {
            var user_id = $('input[name=user_id]').val();
            if (user_id == 0) {
                alert("Bạn cần phải đăng nhập để tạo hồ sơ!")
            } else {
                window.location = 'company/add';
            }
        });
        // Hiển thị ô kết quả rating
        $('.rate-view').each(function() {
            $(this).raty({
                number: 5,
                score: $(this).attr('tag'),
                readOnly: true,
                half     : true,
                size     : 16,
                starHalf : 'small_star-half.png',
                starOff  : 'small_star-off.png',
                starOn   : 'small_star-on.png'
            });
        });
    });
    function printRecruiment() {
        $("div.PrintArea").printArea({
            mode: 'popup'
        });
    }
    function downloadRecruitment() {
        document.location = 'data:Application/octet-stream,filename=file.txt,' + document.getElementById('tab-5').innerHTML;
    }
    function loadComment(company_id) {
        $(".popup-comment").each(function() {
            if ($(this).attr('tag') == company_id) {
                if ($(this).css("display") !== "none") {
                    $(this).hide();
                } else {
                    $(this).show();
                    $('.comment-content[tag=' + company_id + ']').val('');
                }
            } else {
                $(this).hide();
            }
        });
    }
    function sendComment(user_id, company_id) {
        var key = window.event.keyCode;
        isShift = window.event.shiftKey ? true : false;
        // If the user has pressed only enter
        if (key == 13 && !isShift) {
            $('.comment-content').each(function() {
                if ($(this).attr('tag') == company_id) {
                    var comment = $(this).val();
                    $.ajax({
                        url: '/company/comment/' + user_id + '/' + company_id + '/' + comment,
                        type: "POST",
                        success: function(data) {
                            alert("Bình luận thành công!");
                            count = $(".comment[tag = " + company_id + "]").text();
                            new_count = parseInt(count) + 1;
                            if(new_count < 10) {
                                new_count = '0' + new_count;
                            }
                            $(".comment[tag = " + company_id + "]").text(new_count);
                            $(".popup-comment[tag = " + company_id + "]").hide();
                        }
                    });
                }
            });
        }
    }
</script>
<input type="hidden" name="user_id" value="<?php if (isset($_SESSION['userLoggedIn']['id']) && $_SESSION['userLoggedIn']['id'] != "") { echo $_SESSION['userLoggedIn']['id']; } else { echo 0; }?>" />
<input type="hidden" name="filter" value="<?php if (isset($filter) && $filter != "") { echo $filter; } else { echo 0; }?>" />
<?php if ($companys): ?>
    <?php if (!isset($extra)): ?>
        <ul class="thumbnails items">
        <?php endif; ?>	
		<?php if ($bids): ?>
		<?php foreach ($bids as $company): ?>
            <li>
                <a style="display:block; height:155px; position:relative;" rel="companyPopup[ajax]" href="<?php echo base_url('company/view/' . $company['slug'] . '/true'); ?>" data-url="<?php echo '/doanh-nghiep/' . $company['slug']; ?>" >
                    <img style="position:absolute;top:0;left:0;bottom:0;right:0;margin:auto;" <?php /* width="224" height="155" */ ?> src="<?php echo base_url('upload/company/avatar/' . $company['user_id'] . '/' . $company['avatar']); ?>">
                </a>
                <div class="caption">
                    <div style="margin-top: 6px; border:1px dashed #C6C6C6"></div>
                    <h6><?php echo characterLimiter($company['name'], 28); ?></h6>
                    <div style="color: #1C5AC4;"><?php echo getCompanyBusiness($company['businesses']); ?></div>
                </div>
            </li>
        <?php endforeach; ?>
		<?php endif; ?>
        <?php $i = 0; ?>
        <?php foreach ($companys as $company): ?>
            <?php $i++; ?>
            <li style="height: 208px !important;">
                <?php if(isset($company['event']['id']) && $company['event']['id'] != "") { ?>
                    <a style="float: right;margin-right: 32px;" href="/event/search/<?php echo Date("Y"); ?>/<?php echo Date("m"); ?>/<?php echo $company['company_id']; ?>/1">
                        <img class="event-image" src="<?php echo base_url('theme/images/frontend/event.png'); ?>">
                    </a>
                <?php } ?>
                <a style="display:block; height:155px; position:relative;" rel="companyPopup[ajax]" href="<?php echo base_url('company/view/' . $company['slug'] . '/true'); ?>" data-url="<?php echo '/doanh-nghiep/' . $company['slug']; ?>" >
                    <?php if(isset($company['date']) && $company['date'] != "") { ?>
                        <img class="recruiment-image" src="<?php echo base_url('theme/images/frontend/recruiment.png'); ?>">
                    <?php } ?>
                    <img style="position:absolute;top:0;left:0;bottom:0;right:0;margin:auto;" <?php /* width="224" height="155" */ ?> src="<?php echo base_url('upload/company/avatar/' . $company['user_id'] . '/' . $company['avatar']); ?>">
                </a>
                <div class="caption">
                    <div style="margin-top: 6px; border:1px dashed #C6C6C6"></div>
                    <h6><?php echo characterLimiter($company['name'], 28); ?></h6>
                    <div style="color: #1C5AC4;"><?php echo getCompanyBusiness($company['businesses']); ?></div>
                    <div class="caption-left" style="margin-top:5px;">
                        <div class="rate-view" tag="<?php echo $company['rate']; ?>"></div>
                    </div>
                    <div class="caption-right" style="margin-top:3px!important;">
                        <div class="comment" onclick="loadComment('<?php echo $company['company_id']; ?>');" tag="<?php echo $company['company_id']; ?>">
                            <?php echo $company['count_comment']; ?>
                        </div>
                        <div class="detail" onclick="loadDetail('');">
                        </div>
                    </div>
                </div>
                <?php if($i % 4 == 0) { ?>
                    <div class="popup-comment" tag="<?php echo $company['company_id']; ?>" style="margin-top: 10px;margin-left: -236px;">
                <?php } else { ?>
                    <div class="popup-comment" tag="<?php echo $company['company_id']; ?>" style="margin-top: 10px;margin-left: -1px;">
                <?php } ?>
                    <div class="header">
                        <div class="name">
                            <b><?php echo $user['real_name']; ?></b> đang bình luận điều này
                        </div>
                        <div class="date">
                            <?php
                                date_default_timezone_set('Asia/Ho_Chi_Minh');
                                echo date("h:i - d/m/Y");
                            ?>
                        </div>
                    </div>
                    <div class="content">
                        <div class="avatar">
                            <?php if (isset($user['avatar'])) { ?>    
                                <img src="<?php echo getAvatarLink($user['user_id'], $user['avatar']); ?>">
                            <?php } ?>
                        </div>
                        <div class="input" style="padding-bottom: 6px;">
                            <textarea style="width:400px;" onkeydown="sendComment('<?php echo $user['user_id']; ?>', '<?php echo $company['company_id']; ?>')" class="comment-content" tag="<?php echo $company['company_id']; ?>" placeholder="Viết bình luận"></textarea>
                        </div>
                    </div>
                </div>
            </li>
        <?php endforeach; ?>

        <?php if (!isset($extra)): ?>
        </ul>
        <script type="text/javascript">
            // Thanh cuộn load thêm nội dung
            $(document).ready(function() {

                // js.js
                companyPopup();
                var company_extra = 0;
                window.onscroll = function() {
                    scrollY = window.pageYOffset;
                    scrollMaxY = document.documentElement.scrollHeight - document.documentElement.clientHeight;
                    if (scrollY == scrollMaxY) {
                        company_extra++; //s tăng lên một mỗi lần cuộn
                        filter = $('input[name=filter]').val();
                        var url = '<?php echo base_url('company/index'); ?>';
                        var url = url + '\/' + company_extra + '/true/null/' + filter;

                        $.get(url, function(data) {
                            $('.items').append(data); // append to <ul>

                            // js.js
                            companyPopup();

                        });
                    }
                }; // end onscroll

            });
        </script> 
    <?php endif; ?>
<?php else: ?>
    Không tồn tại doanh nghiệp nào.
<?php
endif; // $companys ?>